package gbench.appdemo.medicine.chemistry.particle;

import gbench.common.matlib.rdd.BasePair;

public class Formula {

    /**
     * 成分行分析
     * 
     * @param line
     * @return
     */
    public static BasePair<Particle, Double> parse(String line) {
        String particle_line = line;
        final int offset = line.indexOf("/");
        String ratio_line = null;
        if (offset > 0) {
            particle_line = line.substring(0,offset);
            ratio_line = line.substring(offset + 1);
        }

        final var aa = particle_line.split("_");
        int charge = 0;

        String name = particle_line;
        Particle particle = null;
        if (aa.length > 1) {
            int sgn = 1;
            var s = aa[1];
            if (s.endsWith("-")) {
                sgn = -1;
            }
            s = s.replaceAll("[-\\+]", "");
            charge = Integer.parseInt(s) * sgn;
            particle = Ion.of(aa[0], charge);
        } else {
            particle = Molecule.of(name);
        }

        return BasePair.bp(particle, ratio_line == null ? 0d : Double.parseDouble(ratio_line));
    }

}
